<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Web extends CI_Controller {
   	public function wx(){
   	   $this->load->library('curl');
	   $this->load->model('user_model','user');
	   $appid=$this->config->item("appid");
	   $appsecret=$this->config->item("appscret");
	   $project_url=$this->config->item("project_url");
	   $user=array();
   	   if(isset($_GET['user_id'])){
   	   		$user=$this->user->select_one($_GET['user_id']);
			$this->session->set_userdata($user);

			// $data['user']["lastlogin_time"]=date("Y-m-d h:i:s");
			// $this->user->update($data['user'],"id=".json_decode(json_encode($user))->id);

	        redirect(base_url('front/xuanxiu/index?id='.$_GET['id']));
   	   }
	   //用户登录获取code
	    
	    if (isset($_GET['code'])){
		    $code=$_GET['code'];
		}else{
			$param="";			
			if(isset($_GET['id'])){
				$param="id=".$_GET['id'];
			}
			if(isset($_GET['forward'])){
				$param.="&forward=".$_GET['forward'];
			}
			$param=urlencode($param);
		    redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$project_url."/web/wx?".$param."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
		}
	   //根据code获取access_token
	   $token_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
	   $token_result=$this->curl->ssl(false)->simple_get($token_url);
	   $token=json_decode($token_result);
	   $userinfo_url="https://api.weixin.qq.com/sns/userinfo?access_token=".$token->access_token."&openid=".$token->openid;
	   $userinfo_result=$this->curl->ssl(false)->simple_get($userinfo_url);
       $userinfo=json_decode($userinfo_result);
	   $user["openid"]=$userinfo->openid;
	   $user["nickName"]=$this->user->emoji_encode($userinfo->nickname);
	   $user["gender"]=$userinfo->sex;
	   $user["avatarUrl"]=$userinfo->headimgurl;
	   $user["update_time"]= date('Y-m-d H:i:s',time());
	   $user_list=$this->user->where(array("openid='".$user['openid']."'"))->select_all();
	   if(count($user_list['list'])>0){
	   		$user=$user_list['list'][0];
		    $user["update_time"]= date('Y-m-d H:i:s',time());
	   		$this->user->update($user,"id=".$user['id']);
	   }else{
	   		$user['id']=0;
	   }
		   $this->session->set_userdata($user);
		if($user["telephone"]==""){
			redirect(base_url('front/user/login?id='.$user['id']));
		}
		if(isset($_GET['forward'])){
			redirect($_GET['forward']);
		}
		// $data['user']["lastlogin_time"]=date("Y-m-d h:i:s");
		// $this->user->update($data['user'],"id=".$user['id']);
	    redirect(base_url('web/index?id='.$user['id']));
	}
	public function test(){
		$this->load->library('curl');
		$url = 'http://ag12.ott.bangtv.tv/v30/program/menulist?parentCatgId=teleplay'; 
		$headers = array(
		"versionId"=>"3001",
		"mac"=>"b0:d5:9d:19:eb:b5",
		"agentVendorId"=>"bingotv",
		"did"=>"BC79F0F2924390E41506FDC5B6EF934F",
		"token"=>"0ca1895871aa8a5f096dd391deb57bb2",
		"uid"=>"100004",
		);
	   
	   $token_result=$this->curl->http_header($headers)->simple_get($url);
	   //$this->curl->debug();
	   print_r($token_result);
	}
	public function index(){
		$data["userInfo"]=json_encode($this->session->userdata());
		$data["id"]=$_GET["id"];
		$this->load->view('front/index', $data);
	}
	public function list_movie(){
		$data["menu"]=$_GET["menu"];
		$this->load->view('front/list', $data);
	}
	public function list_live(){
		$data["menu"]=$_GET["menu"];
		$this->load->view('front/livelist', $data);
	}
	public function tv_list(){
		$parentCatgId=$this->input->get('parentCatgId');
		$data['menu']=$parentCatgId;
		echo $data;
		$this->WEB_view("list", $data);	
	}
}